<?php
/**
Various functions about loading data from the database
*/

include('loaddb.php');

/**
	Returns the index of the current scan, or -1 if there aren't any
*/
function getLastScan()
{
	global $mysql;
	$result=$mysql->query("SELECT value FROM int_settings WHERE name='curscan'");
	$myrow=$result->fetch_array();
	if ($myrow)
		return $myrow['value']+0;
	return -1;
}

/**
	Return an array of all of the hosts that are online at this given scan
	
	Each element the following keys:
		id	- the hostid
		name	- the name of the host
		totalsize	- the number of bytes that this host is currently storing
		numsharedfiles	- the number of files that this host is currently storing
*/
function getAllHostStats($scanid)
{
	global $mysql;
	$output=array();
	$result=$mysql->query("select id,hostname,numsharedfiles,totalsize from hosts,host_stats where hosts.id=host_stats.hostid and host_stats.scanid=$scanid order by lower(hostname)");
	while ($myrow=$result->fetch_array())
	{
		$output[]=array('id'=>$myrow['id'], 'name'=>$myrow['hostname'], 'totalsize'=>$myrow['totalsize']+0, 'numsharedfiles'=>$myrow['numsharedfiles']+0);
	}
	return $output;
}

/**
	Return an array of the toplevel directories of this host at the given scan
	
	Each element the following keys:
		id	- the hostid
		name	- the name of the host
		totalsize	- the number of bytes that this directory has
		numsharedfiles	- the number of files that this directory has
*/
function getHostTopDirStats($scanid, $hostid)
{
	global $mysql;
	$output=array();
	$result=$mysql->query("select id,pathname,numsharedfiles,totalsize from paths,path_stats where parentpathid is null and hostid=$hostid and paths.id=path_stats.pathid and path_stats.scanid=$scanid order by lower(pathname)");
	while ($myrow=$result->fetch_array())
	{
		$output[]=array('id'=>$myrow['id'], 'name'=>$myrow['pathname'], 'totalsize'=>$myrow['totalsize']+0, 'numsharedfiles'=>$myrow['numsharedfiles']+0);
	}
	return $output;
}

/**
	Return an array of the subdirectories of this path at the given scan
	
	Each element the following keys:
		id	- the pathid
		name	- the name of the path
		totalsize	- the number of bytes that this directory has
		numsharedfiles	- the number of files that this directory has
*/
function getPathSubdirs($scanid, $pathid)
{
	global $mysql;
	$output=array();
	$result=$mysql->query("select id,pathname,numsharedfiles,totalsize from paths,path_stats where parentpathid=$pathid and paths.id=path_stats.pathid and path_stats.scanid=$scanid order by lower(pathname)");
	while ($myrow=$result->fetch_array())
	{
		$output[]=array('id'=>$myrow['id'], 'name'=>basename($myrow['pathname']), 'totalsize'=>$myrow['totalsize']+0, 'numsharedfiles'=>$myrow['numsharedfiles']+0);
	}
	return $output;
}

/**
	Return an array of the files in this path at the given scan
	
	Each element the following keys:
		id	- the fileid
		name	- the name of the file
		filesize	- the size of this file in bytes
*/
function getPathFiles($scanid, $pathid)
{
	global $mysql;
	$output=array();
	$result=$mysql->query("select id,filename,filesize from files,file_stats where pathid=$pathid and files.id=file_stats.fileid and file_stats.scanid=$scanid order by lower(filename)");
	while ($myrow=$result->fetch_array())
	{
		$output[]=array('id'=>$myrow['id'], 'name'=>$myrow['filename'], 'filesize'=>$myrow['filesize']+0);
	}
	return $output;
}

/**
	Gets an array with information about the given host
	The array has the following keys:
		id	- the hostid
		name	- the name of the host
		totalsize	- the number of bytes that this host is currently storing
		numsharedfiles	- the number of files that this host is currently storing
*/
function getHostInfo($scanid, $hostid)
{
	global $mysql;
	$result=$mysql->query("select id,hostname,numsharedfiles,totalsize from hosts,host_stats where hosts.id=host_stats.hostid and host_stats.scanid=$scanid and host_stats.hostid=$hostid");
	if ($myrow=$result->fetch_array())
	{
		return array('id'=>$myrow['id'], 'name'=>$myrow['hostname'], 'totalsize'=>$myrow['totalsize']+0, 'numsharedfiles'=>$myrow['numsharedfiles']+0);
	}
	return;
}

/**
	Gets an array with information about the given path
	The array has the following keys:
		id	- the hostid
		name	- the name of the path
		hostid	- the hostid that this directory lives in
		parentpathid	- the pathid that this directory lives in, or null
		totalsize	- the number of bytes that this directory has
		numsharedfiles	- the number of files that this directory has
*/
function getPathInfo($scanid, $pathid)
{
	global $mysql;
	$result=$mysql->query("select id,hostid,parentpathid,pathname,numsharedfiles,totalsize from paths,path_stats where paths.id=path_stats.pathid and path_stats.scanid=$scanid and path_stats.pathid=$pathid");
	if ($myrow=$result->fetch_array())
	{
		return array('id'=>$myrow['id'], 'name'=>basename($myrow['pathname']), 'hostid'=>$myrow['hostid'], 'parentpathid'=>is_null($myrow['parentpathid'])?null:$myrow['parentpathid']+0, 'totalsize'=>$myrow['totalsize']+0, 'numsharedfiles'=>$myrow['numsharedfiles']+0);
	}
	return;
}

/**
	Gets an array with information about the given file
	
	The array has the following keys:
		id	- the fileid
		name	- the name of the file
		hostid	- the hostid that this file lives in
		parentpathid	- the pathid that this file lives in
		filesize	- the size of this file in bytes
*/
function getFileInfo($scanid, $fileid)
{
	global $mysql;
	$result=$mysql->query("select id,hostid,parentpathid,filename,filesize from files,file_stats where files.id=file_stats.fileid and file_stats.scanid=$scanid and file_stats.fileid=$fileid");
	if ($myrow=$result->fetch_array())
	{
		return array('id'=>$myrow['id'], 'name'=>$myrow['pathname'], 'hostid'=>$myrow['hostid'], 'parentpathid'=>$myrow['parentpathid']+0,  'filesize'=>$myrow['filesize']+0);
	}
	return;
}